package com.lfm.jdbc.resultset_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * 演示select语句返回ResultSet 返回结果
 */
@SuppressWarnings({"all"})
public class ResultSet_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

        Properties properties = new Properties();
        properties.load(new FileInputStream("mariadb.properties"));

        String url = properties.getProperty("url");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String user = properties.getProperty("user");
        //1. 加载驱动
        Class.forName(driver);

        //2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //3. 得到Statement
        Statement statement = connection.createStatement();
        //4. 组织Sql语句
        /*
            1,周星驰,男,1970-11-11 00:00:00,110
            2,刘德华,男,1970-12-12 00:00:00,110
            3,jack,男,1990-11-11 00:00:00,112
         */
        String sql = "select id, name, sex, borndate from actor;";

        //理解为返回一张表
        ResultSet resultSet = statement.executeQuery(sql);

        //5. 使用while循环取出数据
        while (resultSet.next()) {//让光标往后走, 如果没有行，返回false
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);

        }


        //6. 关闭连接
        resultSet.close();
        statement.close();
        connection.close();


    }
}
